<?php

namespace common\components;

use \Exception;

/**
 * FTP类
 *
 * @author qixiaopeng <qixiaopeng@ledoing.cn>
 */
class Ftp extends \yii\base\Component
{

    /**
     *
     * @var string ftp登录用户名
     */
    public $user;

    /**
     *
     * @var string ftp登录密码
     */
    public $password;

    /**
     *
     * @var string ftp服务器地址
     */
    public $host;

    /**
     * 
     * @var boolean 是否自动连接
     */
    public $autoConnect = false;

    /**
     *
     * @var Ftp ftp连接的实例
     */
    protected $conn;

    public function init()
    {
        parent::init();
        if (empty($this->host) || empty($this->user) || empty($this->password)) {
            throw new Exception('ftp 配置错误');
        }
        if ($this->autoConnect) {
            $this->connect();
        }
    }

    /**
     * 建立一个到ftp服务器的连接
     * @return resource
     */
    public function connect()
    {
        if (!empty($this->conn)) {
            return $this->conn;
        }
        $this->conn = ftp_connect($this->host);
        if (!$this->conn) {
            throw new Exception('can not connected ftp server');
        }
        if (!$this->login()) {
            throw new Exception('can not login to  ftp server');
        }
        if (!ftp_pasv($this->conn, true)) {
            throw new Exception('can not set to pasv');
        }
        return $this->conn;
    }

    /**
     * 登录ftp服务器
     * @param string $user
     * @param string $password
     */
    public function login()
    {
        return ftp_login($this->connect(), $this->user, $this->password);
    }

    /**
     * 得到当前目录
     */
    public function pwd()
    {
        return ftp_pwd($this->connect());
    }

    /**
     * 更改当前目录
     */
    public function chdir($dir)
    {
        return @ftp_chdir($this->connect(), $dir);
    }
    
    /**
     * 检查目录是否存在
     * @param string $dir
     */
    public function isdir($dir)
    {
        $pwd = $this->pwd();
        if (!$this->chdir($dir)) {
            return false;
        }
        return $this->chdir($pwd);
    }

    /**
     * 创建目录
     * @param string $dir 要创建的目录
     * @param boolean $recursive 是否递归创建目录
     * @return boolean
     */
    public function mkdir($dir, $recursive = false)
    {
        if (!$recursive) {
            return ftp_mkdir($this->connect(), $dir);
        }
        $result = false;
        $pwd = $this->pwd();
        $dirs = explode('/', $dir);
        foreach ($dirs as $part) {
            if (!$this->chdir($part)) {
                $result = ftp_mkdir($this->connect(), $part);
                $this->chdir($part);
            }
        }
        $this->chdir($pwd);
        return $result;
    }

    /**
     * 上传文件
     * @param string $local 本地文件全路径
     * @param string $remote 远程文件全路径，如果为空，则和local保持一致
     * @return boolean
     */
    public function put($local, $remote)
    {
        return ftp_put($this->connect(), $remote, $local, FTP_ASCII);
    }

    /**
     * 批量上传文件
     * @param array $localfiles key是远程路径，value是本地路径
     */
    public function puts(array $files)
    {
        if (empty($files)) {
            return false;
        }
        $result = false;
        foreach ($files as $remote => $local) {
            $result = $this->put($local, $remote);
        }
        return $result;
    }

}
